//
//  main.m
//  AES_test
//
//  Created by leeyi on 15/9/7.
//  Copyright (c) 2015年 leeyi. All rights reserved.
//

#import <Cocoa/Cocoa.h>

#import "Encryption.h"

int main(int argc, const char * argv[]) {
    
    NSString *key = @"1q2w3e4r*🐶";
    
    NSString *secret = @"33你好hhh";
    
    //加密
    NSData *plain = [secret dataUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"--plain ======  %@   ---- 原始2进制", plain);
    NSData *cipher = [plain AES256EncryptWithKey:key];
    NSLog(@"--cipher =====  %@   ---- 加密后",cipher);
    NSLog(@"--cipher =====  %@   ---- 64位编码后",[cipher base64StringFromData:cipher length:cipher.length]);
    

    //解密
    NSString *plain2 = @"sIESL9BkHNcPT7Xn5FFWew==";
    NSData *plain3 = [NSData dataFromBase64String:plain2];
    NSLog(@"plain3 === %@",plain3);
    NSData *plain4 = [plain3 AES256DecryptWithKey:key];
    NSLog(@"plain4 === %@",[plain4 description]);
    NSLog(@"plain4 －－－ str === %@", [[NSString alloc] initWithData:plain4 encoding:NSUTF8StringEncoding]);
    // 剪掉2个字节
//    NSMutableData *muData = [[NSMutableData alloc] initWithData:plain3];
//    [muData replaceBytesInRange:NSMakeRange(muData.length - 2, 2) withBytes:NULL length:0];
//    NSLog(@"muData === %@",muData);
//    NSData *plain5 = [muData AES256DecryptWithKey:key];
//    NSLog(@"plain5 === %@",[plain5 description]);
//    NSLog(@"plain5 －－－ str === %@", [[NSString alloc] initWithData:plain5 encoding:NSUTF8StringEncoding]);
    
    
    NSData *testData = [@"123" dataUsingEncoding:NSUTF8StringEncoding];
    NSString *test64Data = [testData base64StringFromData:testData length:testData.length];
    NSLog(@"test64Data ==== %@",test64Data);
    
    NSData *test64Decode = [NSData dataFromBase64String:test64Data];
    NSLog(@"test64Decode ==== %@", [[NSString alloc] initWithData:test64Decode encoding:NSUTF8StringEncoding]);
    
    
    
    return NSApplicationMain(argc, argv);
}
